<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="/style/default/index.css">
  <style>
  </style>
</head>

<body style="background-color:#f8f8f8;">
  <div id="app">

    <!--目录信息dialog-->
    <el-dialog title="参数信息" :visible.sync="dialogItemVisible" width="50%">
      <div style="height:220px;overflow-y:auto;">
        <el-form ref="valueForm" :model="valueData" :rules="rules" label-width="80px" size="mini"
          style="margin-right: 30px;">
          <el-col :span="24">
            <el-form-item label="名称" prop="title">
              <el-input v-model="valueData.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="键" prop="params_key">
              <el-input v-model="valueData.params_key" :disabled="dialogItemDisabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="值类型" prop="value_type">
                <el-select v-model="valueData.value_type" placeholder="请选择" :disabled="dialogItemDisabled" >
                  <el-option v-for="item in optionData.value_type" :key="item.key" :label="item.name" :value="item.key">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          <el-col :span="8">
            <el-form-item label="值" prop="params_value">
              <el-input v-model="valueData.params_value"></el-input>
            </el-form-item>
          </el-col> 
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" v-model="valueData.ext" rows="4"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogItemVisible = false">取 消</el-button>
        <el-button @click="submitEditItem" type="primary" >确 定
        </el-button>
      </div>
    </el-dialog>

    <el-card class="box-card" style="margin-top: 5px;">
      <div slot="header" class="clearfix">
        <span>系统参数设置</span>
        <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-plus" @click="showAddValue()">
          添加新的参数</el-button>
      </div>
      <div>

        <el-table v-loading="tableLoading" :data="valuesSettingsData" style="width: 100%;margin-bottom: 20px;"
          row-key="id" border :height="tableHeight" :max-height="tableHeight" size="mini">
          <el-table-column prop="title" label="名称" width="300" fixed="left">
          </el-table-column>
          <el-table-column prop="params_key" label="键" width="140">
          </el-table-column>
          <el-table-column prop="params_value" label="值" width="80">
          </el-table-column>
          <el-table-column prop="value_type" label="类型" width="80">
            <template slot-scope="scope">
              <span v-for="item in optionData.value_type" v-if="scope.row.value_type === item.key"
                :style="{color:item.color}">{{item.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ext" label="备注" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="80">
            <template slot-scope="scope">
              <span v-for="item in optionData.status" v-if="scope.row.status === item.value"
                :style="{color:item.color}">{{item.name}}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="80">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="editScope(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>
    </el-card>

  </div>
</body>
<script src="https://cdn.staticfile.org/vue/2.6.10/vue.min.js"></script>
<script src="https://cdn.staticfile.org/element-ui/2.11.1/index.js"></script>
<script src="https://cdn.staticfile.org/axios/0.19.0/axios.min.js"></script>
<script src="/js/validate.js"></script>

<script>
  var marginHeight = 140; // 高差数

  var vm = new Vue({
    el: '#app',
    data: function () {
      return {
        tableHeight: window.innerHeight - marginHeight, // 表格高，当窗口改变大小的时候通过 pageResize 修改该值
        tableLoading: true,           // 是否显示加载表格数据加载中画面
        valuesSettingsData: [],       // 参数数据 
        dialogItemVisible: false,     // 是否显示菜单信息对话框
        dialogItemDisabled: false,    // 菜单信息对话框是否禁止编辑 
        valueData: {},                // 菜单数据

        // 表单数据验证
        rules: {
          title: [
            { required: true, message: '该值不能为空', trigger: 'blur' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' }
          ],
          params_key: [
            { required: true, message: '该值不能为空', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ],
          params_value: [
            { required: true, message: '该值不能为空', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          value_type: [
            { required: true, message: '该值不能为空', trigger: 'blur' } 
          ] 
        },
      }
    },
    computed: {
      // 从缓存中读取
      optionData: function () {
        var optionDataStr = sessionStorage.getItem('optionData');
        return JSON.parse(optionDataStr || "{}");
      }
    },
    mounted() {
      // 当窗口大小发生改变的时候系统自动调用
      window.onresize = () => {
        this.pageResize();
      }
    },
    methods: {
      // 改变表格高度
      pageResize() {
        this.tableHeight = window.innerHeight - marginHeight;
      },

      // 显示新增参数 dialog
      showAddValue() {
        this.valueData = {};
        this.valueData.status = 1;
        this.dialogItemDisabled = false;
        this.dialogItemVisible = true;
      },

      // 编辑项目
      editScope(row) {
        this.valueData = JSON.parse(JSON.stringify(row));;
        this.dialogItemDisabled = true;
        this.dialogItemVisible = true;
      },

      // 提交对菜单项的修改
      submitEditItem(e) {
        this.$refs.valueForm.validate((valid) => {
          if (valid) {
            axios.post('/admin/base/edit/sys_settings.bu', this.valueData)
              .then(function (response) {
                console.log(response.data);
                if (response.data.code > 0) {
                  vm.$message.success(response.data.msg);
                  vm.dialogItemVisible = false;
                  loadInitData();
                } else {
                  vm.$message.error(response.data.msg);
                }
              })
              .catch(function (error) {
                vm.$message.error('出错：' + error);
              });
          } else {
            vm.$message.error('错误：提交失败，请检查表单数据是否符合要求！');
            return false;
          }
        });
      }
    },
  });

  // 自动执行，读取数据等
  function loadInitData () {
    axios.get('/admin/base/show/sys_settings!all.bu')
      .then(function (response) {
        if (response.data.code > 0) {
          vm.valuesSettingsData = response.data.bundle;
          vm.tableLoading = false;
        } else if (response.data.code == 0) {
          vm.$message.error("没有数据");
          vm.tableLoading = false;
        } else {
          vm.$message.error(response.data.msg);
          vm.tableLoading = false;
        }
      })
      .catch(function (error) {
        vm.$message.error('获取数据失败！' + error);
      })
  };

  loadInitData();


</script>

</html>